using System;

namespace C3.Proto.Web
{
    public static class UtilityBelt
    {
        public static string GetTimeAgo(DateTime date)
        {
            var now = DateTime.Now;
            
            //if(date.AddDays(30) < now) return date.ToShortDateString();

            var s = date.ToShortDateString();
  
            
            var since = now.Subtract(date);

            if (since.Days > 365)
            {
                var r = Math.Round((decimal) since.Days/365, 0);
                if(r >1)
                    return r + " years ago";
                return r + " year ago";
            }
            if (since.Days > 30 && since.Days < 365)
            {
                var r = Math.Round((decimal) since.Days/30, 0);
                if(r >1)
                    return r + " months ago";
                return r + " month ago";
            }
            if (since.Days > 0 && since.Days < 30)
            {
                var r = since.Days;
                if (r > 1)
                    return r + " days ago";
                return r + " day ago";
            }
            if (since.Hours > 0 && since.Hours < (24 * 30))
            {
                var r = since.Hours;
                if (r > 1)
                    return r + " hours ago";
                return r + " hour ago";
            }
            if (since.Minutes > 0 && since.Minutes < (60 * 24 * 30))
            {
                var r = since.Minutes;
                if (r > 1)
                    return r + " minutes ago";
                return r + " minute ago";
            }
            if (since.Seconds > 0 && since.Seconds < (60 * 60 * 24 * 30))
            {
                var r = since.Seconds;
                if (r > 1)
                    return r + " seconds ago";
                return r + " second ago";
            }
            return (since.Milliseconds > 0 && since.Milliseconds < 2592000000) ? "miliseconds ago" : s;
        }
    }
}